package com.sc.portal.service.impl;

import com.sc.common.pojo.CartItem;
import com.sc.common.pojo.CommonResult;
import com.sc.common.utils.CookieUtils;
import com.sc.common.utils.JsonUtils;
import com.sc.pojo.TbItem;
import com.sc.portal.service.CartService;
import com.sc.portal.service.ItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther sc
 * @Date 2017/11/7
 */
@Service
public class CartServiceImpl implements CartService{
    @Autowired
    private ItemService itemService;

    @Override
    public CommonResult addItem(Long itemId, Integer itemNum, HttpServletRequest request,
                                HttpServletResponse response) {
        boolean have = false;
        List<CartItem> cartItemList = getCartItemList(request);
        for(CartItem item:cartItemList){
            if(item.getItemId().longValue()==itemId){
                item.setNum(item.getNum()+itemNum);
                have=true;
                break;
            }
        }
        //rest服务
        if(!have){
            TbItem item = itemService.getItemById(itemId);
            CartItem c = new CartItem();
            BeanUtils.copyProperties(item,c);
            c.setItemId(itemId);
            c.setNum(itemNum);
            if(StringUtils.isNotBlank(c.getImage())){
                String image = c.getImage();
                String[] strs = image.split(",");
                c.setImage(strs[0]);
            }
            cartItemList.add(c);
        }
        //写入cookie
        CookieUtils.setCookie(request,response,"Cart",JsonUtils.objectToJson(cartItemList),60*60*24*7,true);
        return CommonResult.ok(cartItemList);
    }

    @Override
    public List<CartItem> listCart(HttpServletRequest request) {
        List<CartItem> cartItemList = getCartItemList(request);
        return cartItemList;
    }

    @Override
    public CommonResult updateNum(Long id, int num, HttpServletRequest request, HttpServletResponse response) {
        List<CartItem> cartItemList = getCartItemList(request);
        //根据id查询商品
        for (CartItem item:cartItemList){
            if(item.getItemId().doubleValue() == id){
                item.setNum(num);
                break;
            }
        }
        //写cookie
        CookieUtils.setCookie(request,response,"Cart",JsonUtils.objectToJson(cartItemList),60*60*24*7,true);
        return CommonResult.ok();
    }

    @Override
    public CommonResult deleteItem(Long id, HttpServletRequest request, HttpServletResponse response) {
        List<CartItem> cartItemList = getCartItemList(request);
        for (CartItem item:cartItemList){
            if(item.getItemId().doubleValue() == id){
                cartItemList.remove(item);
                break;
            }
        }
        //写cookie
        CookieUtils.setCookie(request,response,"Cart",JsonUtils.objectToJson(cartItemList),60*60*24*7,true);
        return CommonResult.ok();
    }

    private List<CartItem> getCartItemList(HttpServletRequest request){
        //从cookie中取
        try {
            String json = CookieUtils.getCookieValue(request,"Cart",true);
            List<CartItem> cartItems = JsonUtils.jsonToList(json, CartItem.class);
            return cartItems==null?new ArrayList<>():cartItems;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
